/*
 * The MIT License
 * 
 * Copyright (c) 2010, Shinichi Takano
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */


using System;
using System.Collections.Generic;
using System.Text;
using COM.General;

namespace COM.ROT
{
    public class ROT
    {
        [System.Runtime.InteropServices.DllImport("ole32.dll")]
        private static extern int GetRunningObjectTable(int reserved, out System.Runtime.InteropServices.ComTypes.IRunningObjectTable pprot);

        //[System.Runtime.InteropServices.DllImport("ole32.dll")]
        //private static extern int CreateBindCtx(int reserved, out System.Runtime.InteropServices.ComTypes.IBindCtx ppbc);

        static private System.Runtime.InteropServices.ComTypes.IRunningObjectTable RawROT
        {
            get
            {
                System.Runtime.InteropServices.ComTypes.IRunningObjectTable rot;
                if (GetRunningObjectTable(0, out rot) != 0) throw new Exception("failed in GetRunningObjectTable");
                return rot;
            }
        }

        static private List<System.Runtime.InteropServices.ComTypes.IMoniker> Monikers
        {
            get
            {
                List<System.Runtime.InteropServices.ComTypes.IMoniker> ret = new List<System.Runtime.InteropServices.ComTypes.IMoniker>();

                System.Runtime.InteropServices.ComTypes.IEnumMoniker monikerEnumerator;
                System.Runtime.InteropServices.ComTypes.IMoniker[] monikers = new System.Runtime.InteropServices.ComTypes.IMoniker[1];

                RawROT.EnumRunning(out monikerEnumerator);

                monikerEnumerator.Reset();

                IntPtr numFetched = new IntPtr();
                while (monikerEnumerator.Next(1, monikers, numFetched) == 0)
                {
                    //System.Runtime.InteropServices.ComTypes.IBindCtx ctx;
                    //if (CreateBindCtx(0, out ctx) != 0) throw new Exception("failed in CreateBindCtx");

                    ret.Add(monikers[0]);
                }

                return ret;
            }
        }

        static public List<AutoCOMObject<object>> Objects
        {
            get
            {
                List<AutoCOMObject<object>> ret = new List<AutoCOMObject<object>>();

                foreach (System.Runtime.InteropServices.ComTypes.IMoniker m in Monikers)
                {
                    object runningObjectVal;
                    if (RawROT.GetObject(m, out runningObjectVal) != 0) throw new Exception("failed in GetObject");

                    ret.Add(new AutoCOMObject<object>(runningObjectVal));
                }

                return ret;
            }
        }
    }
}
